//
//  ApplyDrawViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/9/28.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "ApplyDrawViewController.h"
#import <MBProgressHUD.h>
#import "DrawSuccessView.h"
#import "DrawMoneyNet.h"
#import "QHKeyboard.h"

@interface ApplyDrawViewController ()
@property (weak, nonatomic) IBOutlet UITextField *applyMoney;
@property (weak, nonatomic) IBOutlet UILabel *nowMoney;
@property (weak, nonatomic) IBOutlet UILabel *canDrawMoney;

@end

@implementation ApplyDrawViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"申请提款";
    self.nowMoney.text = [NSString stringWithFormat:@"现有金额：￥%.2f",self.totalProfit];
    self.canDrawMoney.text = [NSString stringWithFormat:@"可用金额：￥%.2f",self.drawProfit];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyBoard)];
    [self.view addGestureRecognizer:tap];
    _applyMoney.inputView = [[QHKeyboard alloc] init];
}

- (void)dismissKeyBoard {
    [self.view endEditing:YES];
}

- (IBAction)commitApply:(id)sender {
    NSString *money = self.applyMoney.text;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    if (!money || [money length] == 0) {
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"请输入申请提现的金额！";
        [hud hideAnimated:YES afterDelay:2];
        return;
    }
    [DrawMoneyNet DrawMoney:money completer:^(id result, NSError *error) {
        if (result) {
            DrawSuccessView *successView = [DrawSuccessView drawSuccessView];
            UIWindow *window = (UIWindow *)[UIApplication sharedApplication].delegate.window;
            [successView show:window animation:YES];
            [hud setHidden:YES];
            
            CGFloat nowProfit = self.totalProfit - [money floatValue];
            CGFloat canDraw = self.drawProfit - [money floatValue];
            self.nowMoney.text = [NSString stringWithFormat:@"现有金额：￥%.2f",nowProfit];
            self.canDrawMoney.text = [NSString stringWithFormat:@"可用金额：￥%.2f",canDraw];
            
        }else {
            hud.mode = MBProgressHUDModeText;
            hud.label.text = [error localizedDescription];
            [hud hideAnimated:YES afterDelay:2];
        }
    }];
    
    
}

@end
